package org.king.mapper;

import org.apache.ibatis.annotations.*;
import org.king.entity.Activity;
import org.king.entity.vo.ActivityEntity;
import org.king.entity.vo.Comment;

import java.time.LocalDateTime;
import java.sql.Timestamp;
import java.util.List;

@Mapper
public interface ActivityMapper {
    //activity有关的
    @Select("select * from activities where activity_id=#{activityId}")
    Activity getActivity(@Param("activityId") int activityId);
    //查找用户自己发布的活动
    @Select("select * from activities where create_id = #{create_id}")
    List<Activity> getUserActivities(@Param("create_id") int create_id);

    @Select("select * from activities")
    List<Activity> getAllActivities();

    @Insert("insert into activities(name,activity_describe,begin_time,end_time,address,create_id) " +
            "values(#{name},#{activity_describe},#{begin_time},#{end_time},#{address},#{create_id})")
    @Options(useGeneratedKeys = true, keyProperty = "activity_id", keyColumn = "activity_id")
    int addActivity(Activity activity);

    @Delete("delete from activities where activity_id=#{activityId}")
    int deleteActivity(@Param("activityId") int activityId);

    @Update("update activities set name=#{name},activity_describe=#{describe},begin_time=#{begin}," +
            "end_time=#{end},address=#{address} where activity_id=#{activityId} ")
    int updateActivity(@Param("name") String name,
                       @Param("describe") String describe,
                       @Param("begin")Timestamp begin,
                       @Param("end") Timestamp end,
                       @Param("address") String address,
                       @Param("activityId") int activityId);

    @Results({
            @Result(property = "id", column = "activity_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "description", column = "activity_describe"),
            @Result(property = "beginTime", column = "begin_time"),
            @Result(property = "endTime", column = "end_time"),
            @Result(property = "address", column = "address"),
            @Result(property = "creator", column = "create_id")
    })
    @Select("SELECT * FROM activities WHERE " +
            "(begin_time <= #{endDate} AND end_time >= #{startDate})")
    List<ActivityEntity> findByDateRange(
            @Param("startDate") LocalDateTime startDate,
            @Param("endDate") LocalDateTime endDate
    );

    //comment有关的
    @Insert("insert into comment(user_id,activity_id,text) " +
            "values (#{user_id},#{activity_id},#{text})")
    @Options(useGeneratedKeys = true, keyColumn = "comment_id", keyProperty = "comment_id")
    int addComment(Comment comment);

    @Update("update comment set text=#{comment} where comment_id=#{comment_id}")
    int updateComment(@Param("comment") String comment, @Param("comment_id") int comment_id);

    @Select("select * from comment where activity_id=#{activityId}")
    List<Comment> getCommentByActivity(@Param("activityId") int activityId);


    //user_join_activity有关的
    @Insert("insert into user_join_activity(user_id,activity_id,score) values(#{uId},#{AId},0)")
    int addUserJoinActivity(@Param("uId") int userId,@Param("AId") int activityId);

    @Update("update user_join_activity set score=#{score} where activity_id=#{AId} and user_id=#{UId}")
    int updateUserJoinActivityScore(@Param("AId") int activityId,@Param("UId") int userId,@Param("score") int score);

    @Delete("delete from user_join_activity where activity_id=#{AId} and user_id=#{UId}")
    int deleteUserJoinActivity(@Param("AId") int activityId,@Param("UId") int userId);

    @Select("select count(*) from user_join_activity where activity_id=#{AId} and user_id=#{UId}")
    int getUserJoinActivity(@Param("AId") int activityId,@Param("UId") int userId);


    @Select("SELECT COUNT(*) FROM user_join_activity WHERE activity_id = #{activityId}")
    int getParticipantCount(@Param("activityId") int activityId);

    // 查询活动的平均评分（只计算已评分的用户）
    @Select("SELECT AVG(score) FROM user_join_activity WHERE activity_id = #{activityId} AND score > 0")
    Double getAverageRating(@Param("activityId") int activityId);

}
